package com.irishud.hudnavi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

/*
FileTool mFT = new FileTool();
mFT.createFile("/cache/knightfile");
mFT.createFolder("/cache/knightfolder");
*/

/*
String fileName = Environment.getExternalStorageDirectory()+"/screen.txt";
BufferedReader reader = null;
try {
	File file = new File(fileName);
	if(!file.exists())
		return;
	reader = new BufferedReader(new FileReader(file));
	String tempString = null;
	int line = 1;
	while ((tempString = reader.readLine()) != null) {
		// ��ʾ�к�
		System.out.println("line " + line + ": " + tempString);
		//ȥ�����кͿո��
		tempString = tempString.replace("\\s", "");
		tempString = tempString.replace("\n", "");
		Log.d(TAG,"y:" + Integer.valueOf(tempString));
		Log.d("FileManager", "��ȡ�ļ���ֵƫ����");
		line++;
	}
}catch (IOException e) {
	Log.e(TAG,"k.log print IOException");
	e.printStackTrace();
} finally {
	if (reader != null) {
		try {
			reader.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}*/

public class FileTool {
	private static final String TAG = "FileTool";

	public void createFolder(String foldername) {
		Log.d(TAG,"createFolder");
		File dirFirstFolder = new File(foldername); 
        if(!dirFirstFolder.exists()) {
          dirFirstFolder.mkdirs();
        }
	}
	
	public void createFile(String filename) {
		Log.d(TAG,"createFile");
		File file = new File(filename);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
	}
	
	public boolean writeFile(String filename,String str) {
		File writeFile = new File(filename);
		FileWriter fw;
        try {
            fw = new FileWriter(writeFile);
            fw.write(str); 
            fw.close();
        } catch (IOException e) {
              e.printStackTrace();
              return false;
        }
        return true;
	}
	
	public String readlinuxfile(String filepath){
		String str = "";
		try{
			File readFile = new File(filepath);
	        FileReader fr = new FileReader(readFile);
	        try{
	        	char [] buffer = null;
	        	int v1 = fr.read();
	        	int v2 = fr.read();
	        	int v3 = fr.read();
	        	int v4 = fr.read();
	        	if(("" + v1 + v2 + v3 + v4).equals("70826969"))
	        		return "free";
	        	else
	        		return "busy";
			} catch (IOException e) {
				e.printStackTrace();
			}
        } catch (FileNotFoundException e){
        	e.printStackTrace();
        }
		return "busy";
	}
	
	public boolean writelinuxfile(String filepath,String str) {
		File writeFile = new File(filepath);
        FileWriter fw;
        
        String state="";
        do {
            state = readlinuxfile(filepath);
            Log.d(TAG,"k.log state:" + state);
            if(state.equals("busy")) {
            	try {
                    Thread.sleep(500);
                } catch (Exception e) {
                      e.printStackTrace();
                }
            } else {
            	break;
            }
        } while(true);
        
        try {
            fw = new FileWriter(writeFile);
            fw.write(str); 
            fw.close();
        } catch (IOException e) {
              e.printStackTrace();
              return false;
        }
        return true;
	}
	
	public void testlinuxfilerw() {
		thread_test.run();
	}
    Thread thread_test = new Thread(new Runnable() {
    	String path = "/sys/bus/platform/drivers/commom2phase/rwreg";
    	String [] str = {"2 1","1 50",
    					"2 0","1 A0",
    					"2 1","1 80",
    					"2 0","1 60",
    					"2 1","1 40",
    					"2 0","1 10",};
        public void run() {
            FileTool mFT = new FileTool();
            int i = 0;
            while (true) {
                try {
                    Log.d(TAG,"k.log cycle:" + i);
                    mFT.writelinuxfile(path,str[i++]);
                    Thread.sleep(1);
                    mFT.writelinuxfile(path,str[i++]);
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(i > 11)
                	i = 0;
            }
        }
    });
}
